﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace bigormscreensaver
{
    public partial class SettingsForm : Form
    { 
        private Settings settings;
        #region Init, mode and general
        public SettingsForm(Settings settings)
        {
            this.settings = settings;
            InitializeComponent();
        }
        public override string ToString()
        {
            return settings.ToString();
        }
        private bool bModeEditable = false;
        private void SettingsForm_Load(object sender, EventArgs e)
        {
            tabControl1.Controls.Remove(tabPageDebug);
            bModeEditable = false;
            Image_Initialize(sender, e);
            LinkedIn_Initialize(sender, e);
            Network_Initialize(sender, e);
            Feed_Initialize(sender, e);
            Text_Initialize(sender, e);
            switch (settings.Mode_SelectedIndex)
            {
                case 0: radioButtonImage.Checked = true; break;
                case 1: radioButtonLinkedin.Checked = true; break;
                case 2: radioButtonNetwork.Checked = true; break;
                case 3: radioButtonFeed.Checked = true; break;
                case 4: radioButtonText.Checked = true; break;
                default:
                    bModeEditable = true; // Save (4) to registry
                    radioButtonText.Checked = true; 
                    break;
            }
            new ScreensaverForm(settings, labelPreviewDisplay.Handle).Show();
            bModeEditable = true;
        }
        private void buttonOk_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonImage.Checked)
                tabControl1.SelectedIndex = 0;
            else if (radioButtonLinkedin.Checked)
                tabControl1.SelectedIndex = 1;
            else if (radioButtonNetwork.Checked)
                tabControl1.SelectedIndex = 2;
            else if (radioButtonFeed.Checked)
                tabControl1.SelectedIndex = 3;
            else if (radioButtonText.Checked)
                tabControl1.SelectedIndex = 4;
            if (bModeEditable)
            {
                settings.Mode_SelectedIndex = tabControl1.SelectedIndex;
                foreach (var form in Application.OpenForms)
                    if (form is ScreensaverForm)
                    {
                        ScreensaverForm sf = ((ScreensaverForm)form);
                        sf.bModeImage = tabControl1.SelectedIndex == 0;
                        sf.Image_Show(sf.bModeImage);
                        sf.bModeLinkedin = tabControl1.SelectedIndex == 1;
                        sf.Linkedin_Show(sf.bModeLinkedin);
                        sf.bModeNetwork = tabControl1.SelectedIndex == 2;
                        sf.Network_Show(sf.bModeNetwork);
                        sf.bModeFeed = tabControl1.SelectedIndex == 3;
                        sf.Feed_Show(sf.bModeFeed);
                        sf.bModeText = tabControl1.SelectedIndex == 4;
                        sf.Text_Show(sf.bModeText);
                    }
            }
        }
        #endregion Init, mode and general
        #region Image
        private bool bImageEditable = false;
        private void Image_Initialize(object sender, EventArgs e)
        {
            bImageEditable = false;
            image_paths_textBox.Lines = settings.Image_Paths;
            image_path_first_textBox.Text = settings.Image_PathFirst;
            image_displayStyle_comboBox.SelectedIndex = image_displayStyle_comboBox.Items.IndexOf(settings.Image_DisplayStyle);
            text_colorBg_colorDialog.Color = settings.Image_Backcolor;
            image_interval_textBox.Text = settings.Image_IntervalString;
            image_interval_progressBar.Minimum = 0;
            image_interval_progressBar.Maximum = Util.SelectableTime.Length - 1;
            Image_SetProgressbarPosition(image_interval_textBox.Text);
            Image_UpdatePreview();
            bImageEditable = true;
        }
        private void image_paths_textBox_TextChanged(object sender, EventArgs e)
        {
            if (bImageEditable)
                settings.Image_Paths = image_paths_textBox.Lines;
        }
        private void image_path_first_textBox_TextChanged(object sender, EventArgs e)
        {
            if (bImageEditable)
                settings.Image_PathFirst = image_path_first_textBox.Text;
        }
        private void image_displayStyle_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bImageEditable)
                settings.Image_DisplayStyle = image_displayStyle_comboBox.Text;
        }
        private void image_backcolor_linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (bImageEditable)
                if (image_colorBg_colorDialog.ShowDialog() == DialogResult.OK)
                {
                    settings.Image_Backcolor = image_colorBg_colorDialog.Color;
                    Image_UpdatePreview();
                }
        }
        private void image_interval_textBox_TextChanged(object sender, EventArgs e)
        {
            if (bImageEditable)
            {
                settings.Image_IntervalString = image_interval_textBox.Text;
                Image_UpdatePreview();
                if (sender != image_interval_progressBar)
                    Image_SetProgressbarPosition(image_interval_textBox.Text);
            }
        }
        private void Image_SetProgressbarPosition(string sText)
        {
            TimeSpan tsActive = Util.UTime.FromString(sText);
            for (int i = 0; i < Util.SelectableTime.Length; i++)
                if (Util.SelectableTime[i].tsDuration >= tsActive)
                {
                    image_interval_progressBar.Value = i;
                    break;
                }
        }
        private void Image_UpdatePreview()
        {
            foreach (var form in Application.OpenForms)
                if (form is ScreensaverForm)
                    ((ScreensaverForm)form).Image_Show(((ScreensaverForm)form).bModeImage);
        }
        private int image_interval_progressBarLastMousePosition = -1;
        private void image_interval_progressBar_Click(object sender, EventArgs e)
        { // set position by clicking
            if (image_interval_progressBarLastMousePosition != -1)
                image_interval_textBox.Text = Util.SelectableTime[image_interval_progressBarLastMousePosition].ToString();
        }
        private void image_interval_progressBar_MouseMove(object sender, MouseEventArgs e)
        {
            double fPos = (float)e.X / (image_interval_progressBar.Width);
            if (fPos >= 1)
                fPos = .99999;
            else if (fPos < 0)
                fPos = 0;
            image_interval_progressBarLastMousePosition = (int)Math.Truncate(Util.SelectableTime.Length * fPos);
            if (e.Button == System.Windows.Forms.MouseButtons.Left) // set position by dragging
                image_interval_textBox.Text = Util.SelectableTime[image_interval_progressBarLastMousePosition].ToString();
        }
        #endregion Image
        #region Feed
        private bool bFeedEditable = false;
        private void Feed_Initialize(object sender, EventArgs e)
        {
            bFeedEditable = false;
            bFeedEditable = true;
        }
        #endregion Feed
        #region Network
        private bool bNetworkInEditable = false;
        private void Network_Initialize(object sender, EventArgs e)
        {
            bNetworkInEditable = false;
            bNetworkInEditable = true;
        }
        #endregion Network
        #region LinkedIn
        private bool bLinkedInEditable = false;
        private void LinkedIn_Initialize(object sender, EventArgs e)
        {
            bLinkedInEditable = false;
            //User id
            //User password
            bLinkedInEditable = true;
        }
        #endregion LinkedIn
        #region Text
        private bool bTextEditable = false;
        private void Text_Initialize(object sender, EventArgs e)
        {
            bTextEditable = false;
            text_caption_textBox.Text = settings.Text_Caption;
            text_face_comboBox.Items.Clear();
            foreach (string fontFace in settings.font_availableFaces)
                text_face_comboBox.Items.Add(fontFace);
            text_face_comboBox.SelectedIndex = text_face_comboBox.Items.IndexOf(settings.Text_FontFace);
            text_size_textBox.Text = settings.Text_FontSize.ToString();
            text_color_colorDialog.Color = settings.Text_Color;
            text_colorBg_colorDialog.Color = settings.Text_Backcolor;
            text_interval_textBox.Text = settings.Text_IntervalString;
            text_interval_progressBar.Minimum = 0;
            text_interval_progressBar.Maximum = Util.SelectableTime.Length - 1;
            Text_SetProgressbarPosition(text_interval_textBox.Text);
            Text_UpdatePreview();
            bTextEditable = true;
        }

        private void Text_UpdatePreview()
        {
            text_preview_label.Text = settings.Text_Caption;
            text_preview_label.Font = settings.Text_Font;
            text_preview_label.ForeColor = settings.Text_Color;
            text_preview_label.BackColor = settings.Text_Backcolor;
            foreach (var form in Application.OpenForms)
                if (form is ScreensaverForm)
                    ((ScreensaverForm)form).Text_Show(((ScreensaverForm)form).bModeText);
        }

        private void text_caption_textBox_TextChanged(object sender, EventArgs e)
        {
            if (bTextEditable)
            {
                settings.Text_Caption = text_caption_textBox.Text;
                Text_UpdatePreview();
            }
        }

        private void text_face_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bTextEditable)
                if (text_face_comboBox.SelectedIndex > -1)
                {
                    settings.Text_FontFace = (string)text_face_comboBox.Items[text_face_comboBox.SelectedIndex];
                    Text_UpdatePreview();
                }
        }

        private void text_size_textBox_TextChanged(object sender, EventArgs e)
        {
            long iFs;
            if (long.TryParse(text_size_textBox.Text, out iFs))
            {
                if (bTextEditable)
                {
                    settings.Text_FontSize = iFs;
                    Text_UpdatePreview();
                }
            }
        }

        private void text_color_linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (bTextEditable)
                if (text_color_colorDialog.ShowDialog() == DialogResult.OK)
                {
                    settings.Text_Color = text_color_colorDialog.Color;
                    Text_UpdatePreview();
                }
        }

        private void text_colorBg_linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (bTextEditable)
                if (text_colorBg_colorDialog.ShowDialog() == DialogResult.OK)
                {
                    settings.Text_Backcolor = text_colorBg_colorDialog.Color;
                    Text_UpdatePreview();
                }
        }

        private void text_interval_textBox_TextChanged(object sender, EventArgs e)
        {
            if (bTextEditable)
            {
                settings.Text_IntervalString = text_interval_textBox.Text;
                Text_UpdatePreview();
                if (sender != text_interval_progressBar)
                    Text_SetProgressbarPosition(text_interval_textBox.Text);
            }
        }

        private void Text_SetProgressbarPosition(string sText)
        {
            TimeSpan tsActive = Util.UTime.FromString(sText);
            for (int i = 0; i < Util.SelectableTime.Length; i++)
                if (Util.SelectableTime[i].tsDuration >= tsActive)
                {
                    text_interval_progressBar.Value = i;
                    break;
                }
        }

        private int text_interval_progressBarLastMousePosition = -1;

        private void text_interval_progressBar_Click(object sender, EventArgs e)
        { // set position by clicking
            if (text_interval_progressBarLastMousePosition != -1)
                text_interval_textBox.Text = Util.SelectableTime[text_interval_progressBarLastMousePosition].ToString();
        }

        private void text_interval_progressBar_MouseMove(object sender, MouseEventArgs e)
        {
            double fPos = (float)e.X / (text_interval_progressBar.Width);
            if (fPos >= 1)
                fPos = .99999;
            else if (fPos < 0)
                fPos = 0;
            text_interval_progressBarLastMousePosition = (int)Math.Truncate(Util.SelectableTime.Length * fPos);
            if (e.Button == System.Windows.Forms.MouseButtons.Left) // set position by dragging
                text_interval_textBox.Text = Util.SelectableTime[text_interval_progressBarLastMousePosition].ToString();
        }
        #endregion Text
        #region Debug
        #region Debug general
        public void Show_Debug()
        {
            Show();
            buttonDebug.Show();
            buttonDebug_Click(buttonDebug, new EventArgs());
        }

        private ScreensaverForm debug_image_ScreensaverForm = null;
        private void debug_messageAdd(string[] sa)
        {
            string[] sl = new string[debug_textBox.Lines.Length + sa.Length];
            if (debug_textBox.Lines.Length > 0)
                debug_textBox.Lines.CopyTo(sl, 0);
            if (sa.Length > 0)
                sa.CopyTo(sl, debug_textBox.Lines.Length);
            debug_textBox.Lines = sl;
        }

        private void debug_messageAdd(string s)
        {
            debug_messageAdd(s.Split("\r".ToCharArray()));
        }

        private void buttonDebug_Click(object sender, EventArgs e)
        {
            if (tabControl1.Controls.IndexOf(tabPageDebug) == -1)
            {
                debug_messageAdd("Debug open");
                tabControl1.Controls.Add(tabPageDebug);
                if (tabControl1.SelectedIndex != 5)
                {
                    tabControlDebug.SelectedIndex = tabControl1.SelectedIndex;
                    tabControl1.SelectedIndex = 5;
                }
                bool bAppVisible = false;
                foreach (Form f in Application.OpenForms)
                    if (f is AppForm)
                        bAppVisible = true;
                if (!bAppVisible)
                    new AppForm(settings).Show();
            }
            else
            {
                debug_messageAdd("Debug close");
                tabControl1.Controls.Remove(tabPageDebug);
            }
        }
        private void debug_button1_Click(object sender, EventArgs e)
        {
            if (debug_image_ScreensaverForm != null)
                debug_button2_Click(sender, e);
            Rectangle bounds = new Rectangle(Left + Width, Screen.AllScreens[0].Bounds.Top, Screen.AllScreens[0].Bounds.Width - Left - Width, Screen.AllScreens[0].Bounds.Bottom);
            debug_messageAdd(string.Format("ScreensaverForm {0}", bounds));
            debug_image_ScreensaverForm = new ScreensaverForm(settings, bounds);
            debug_image_ScreensaverForm.Show();
            debug_image_ScreensaverForm.TopMost = false;
            this.Focus();
            Cursor.Show();
        }

        private void debug_button2_Click(object sender, EventArgs e)
        {
            if (debug_image_ScreensaverForm != null)
            {
                debug_messageAdd(string.Format("ScreensaverForm close"));
                debug_image_ScreensaverForm.Close();
            }
        }

        private void debug_button3_Click(object sender, EventArgs e)
        {
            debug_messageAdd(settings.ToString());
        }

        private void debug_button4_Click(object sender, EventArgs e)
        {
            debug_textBox.Text = "";
        }

        #endregion Debug general
        #region Debug image
        private void debug_image_button5_Click(object sender, EventArgs e)
        {
            if (debug_image_ScreensaverForm != null)
            {
                debug_messageAdd("\nPaths:");
                debug_messageAdd(settings.Images.ImageSearchPaths.ToArray());
                debug_messageAdd("\nImages: (default " + settings.Images.Default + ")");
                List<string> found = new List<string>();
                foreach (string s in settings.Images)
                    found.Add(s);
                debug_messageAdd(found.ToArray());
            }
        }

        private void debug_image_button6_Click(object sender, EventArgs e)
        {
            if (debug_image_ScreensaverForm != null)
            {
                debug_messageAdd("HTML");
                foreach (string s in settings.Image_Paths)
                    try
                    {
                        debug_messageAdd(s);
                        debug_messageAdd(new StreamReader(new WebClient().OpenRead(s)).ReadToEnd());
                    }
                    catch (Exception) { }                
            }
        }

        #endregion Debug image
        #region Debug linkedin
        private void debug_ln_button5_Click(object sender, EventArgs e)
        {
            //
        }

        private void debug_ln_button6_Click(object sender, EventArgs e)
        {
            //
        }

        #endregion Debug linkedin
        #region Debug net
        private void debug_net_button5_Click(object sender, EventArgs e)
        {
            //
        }

        private void debug_net_button6_Click(object sender, EventArgs e)
        {
            //
        }

        #endregion Debug net
        #region Debug feed
        private void debug_feed_button5_Click(object sender, EventArgs e)
        {
            //
        }

        private void debug_feed_button6_Click(object sender, EventArgs e)
        {
            //
        }

        #endregion Debug feed
        #region Debug txt
        private void debug_txt_button5_Click(object sender, EventArgs e)
        {
            //
        }

        private void debug_txt_button6_Click(object sender, EventArgs e)
        {
            //
        }
        #endregion Debug txt

        #endregion Debug

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            foreach (var screen in Screen.AllScreens)
                new ScreensaverForm(settings, screen.Bounds).Show();
        }
    }
}